package gui;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Camera 
{
	private int x, y, height, width;
	private BufferedImage fullBackground; 
	private BufferedImage frame;
	private boolean shouldStopX;
	private boolean shouldStopY;
	
	private Point lastLocation;
	
	public Camera(int x, int y, int width, int height)
	{
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		
		frame = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		shouldStopX = false;
		shouldStopY = false;
		
		lastLocation = new Point(0, 0);
	}
	
	public void setBackground(BufferedImage bg)
	{
		fullBackground = bg;
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public BufferedImage getFrame()
	{	
		BufferedImage subImg = fullBackground.getSubimage(x, y, width, height);
		
		Graphics2D g = frame.createGraphics();
		g.clearRect(0, 0, width, height);
		g.drawImage(subImg, 0, 0, width, height, null);
		
		return frame;
	}
	
	public void moveX(float distance)
	{
		//are we stuck?
		if(shouldStopX) {
			shouldStopX = false;
			setX((int) lastLocation.getX());
		}
		else if(x + distance > 0 && x + distance + width < fullBackground.getWidth()) {
			lastLocation.setLocation(x, lastLocation.getY());
			x+=distance;
		}
	}
	
	public void moveY(float distance)
	{
		if(shouldStopY) {
			shouldStopY = false;
			setY((int) lastLocation.getY());
		}
		else if(y + distance > 0 && y + distance + height < fullBackground.getHeight()) {
			lastLocation.setLocation(lastLocation.getX(), y);
			y+=distance;
		}
	}
	
	public void stop(boolean x)
	{
		if(x) {
			shouldStopX = true;
		}
		else {
			shouldStopY = true;
		}
	}
}
